home *** CD-ROM | disk | FTP | other *** search
/ Suzy B Software 2 / Suzy B Software CD-ROM 2 (1994).iso / prntutil / gemgs13 / util.c < prev   
C/C++ Source or Header  |  1995-04-25  |  6KB  |  266 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <aesbind.h>
  5. #include <vdibind.h>
  6. #include "ps.h"
  7.  
  8. #define MAXLEN    80
  9. #define MIN(x, y) ((x <= y) ? x : y)
  10.  
  11. extern struct document *docinfo;
  12.  
  13. /* Pathfind(path, file, sep) searches for 'file' in the path given
  14.  * by the envionment variable named in 'env'. Sep specifies the
  15.  * path separator. It returns a pointer to the filename if it is
  16.  * found, and a NULL if not.
  17.  */
  18.  
  19. char *pathfind(char *env, char *file, char *sep)
  20. {
  21.     FILE *fp;
  22.  
  23.     char path[2*MAXLEN];
  24.     static char name[MAXLEN];
  25.  
  26.     char *ptr;
  27.     char *presub();
  28.  
  29.     if ((fp = fopen(file, "r")) == NULL) {
  30.  
  31.     if ((ptr = getenv(env)) != NULL) {
  32.         strcpy(path, ptr);
  33.     }
  34.     else {
  35.         return NULL;
  36.     }
  37.  
  38.     if ((ptr = presub(path, sep)) == NULL) {
  39.         return NULL;
  40.     }
  41.  
  42.     do {
  43.         strcpy(name, ptr);
  44.  
  45.         if (name[strlen(name)-1] != '\\' ||
  46.         name[strlen(name)-1] != '/') strcat(name, "\\");
  47.  
  48.         strcat(name, file);
  49.  
  50.         if ((fp = fopen(name, "r")) != NULL) {
  51.         fclose(fp);
  52.         return name;
  53.         }
  54.  
  55.     } while ((ptr = presub(NULL, sep)) != NULL);
  56.  
  57.     }
  58.     else {
  59.     fclose(fp);
  60.     return file;
  61.     }
  62. }
  63.  
  64. /* Presub(s, subs) searches the string s for the substring subs. Presub
  65.  * returns a pointer to the NULL terminated substring which immediately
  66.  * preceeds subs. If presub is called again with a NULL pointer for s, 
  67.  * it returns a pointer to the substring between the previous subs and
  68.  * the current subs. If subs is a null character, presub returns a pointer
  69.  * to the portion of s between the previous subs and the end of s.
  70.  */
  71.  
  72. char *presub(s, subs)
  73. char *s, *subs;
  74. {
  75.     int sublen;
  76.     char *mptr;
  77.     static int count;
  78.     static char match[MAXLEN], *ptr;
  79.  
  80.     if (s != NULL) {
  81.         ptr = s;
  82.         count = 0;
  83.     }
  84.  
  85.     mptr = match + count;
  86.     if ((sublen = strlen(subs)) == 0) {
  87.         strcpy(mptr, ptr);
  88.         return (mptr);
  89.     }
  90.  
  91.     while (*ptr != '\0') {
  92.         if (strncmp(ptr, subs, sublen) == 0) {
  93.             match[count] = '\0';
  94.             ptr += sublen;
  95.             ++count;
  96.             return (mptr);
  97.         }
  98.         match[count] = *ptr;
  99.         ++ptr;
  100.         ++count;
  101.         
  102.     }
  103.  
  104.     if ((match + count) > mptr) {
  105.         match[count] = *ptr;
  106.         ++count;
  107.         return (mptr);
  108.  
  109.     }
  110.  
  111.     return (NULL);
  112.  
  113. }
  114.  
  115. int page_filter(char* input, char *pagestr, char *output)
  116. {
  117.     void pscopy();
  118.  
  119.     char *strptr1, *strptr2;
  120.     char *presub();
  121.  
  122.     int num_list=0, num_pages, page_order, count;
  123.     int    pagelist[40][2];
  124.  
  125.     FILE *infp, *outfp;
  126.  
  127.     struct page *pageptr1, *pageptr2;
  128.  
  129.     if ((strptr1 = pathfind("GS_LIB", input, ",")) == NULL) {
  130.     return -1;
  131.     }
  132.     if ((infp = fopen(strptr1, "r")) == NULL) {
  133.     return -1;
  134.     }
  135.     if ((outfp = fopen(output, "wb")) == NULL) {
  136.     return -1;
  137.     }
  138.  
  139.     if ((strptr1 = presub(pagestr, ",")) == NULL) {
  140.     return -1;
  141.     }
  142.  
  143.     do {
  144.  
  145.     if ((strptr2 = strchr(strptr1, '-')) == NULL) {
  146.         pagelist[num_list][1] = atoi(strptr1);
  147.         pagelist[num_list++][2] = atoi(strptr1);
  148.     }
  149.     else {
  150.         *strptr2 = '\0';
  151.         pagelist[num_list][1] = atoi(strptr1);
  152.         pagelist[num_list++][2] = atoi(strptr2+1);
  153.     }
  154.  
  155.     } while ((strptr1 = presub(NULL, ",")) != NULL);
  156.  
  157.     num_pages = docinfo->numpages;
  158.     page_order = docinfo->pageorder;
  159.  
  160.     /* Copy to the beginning of the first page into PAGEFILE. */
  161.  
  162.     pageptr1 = &docinfo->pages[0];
  163.     pscopy(infp, outfp, 0, pageptr1->begin - 1);
  164.  
  165.     /* Copy the requested pages into PAGEFILE. */
  166.  
  167.     for (count=0; count < num_list; count++) {
  168.     pagelist[count][2] = MIN(pagelist[count][2], num_pages);
  169.  
  170.     if (pagelist[count][1] <= pagelist[count][2]) {
  171.  
  172.         if (page_order == ASCEND) {
  173.         pageptr1 = &docinfo->pages[pagelist[count][1] - 1];
  174.         pageptr2 = &docinfo->pages[pagelist[count][2] - 1];
  175.         }
  176.         else if (page_order == DESCEND) {
  177.         pageptr2 = &docinfo->pages[num_pages - pagelist[count][1]];
  178.         pageptr1 = &docinfo->pages[num_pages - pagelist[count][2]];
  179.         }
  180.         else {
  181.         return -1;
  182.         }
  183.  
  184.         pscopy(infp, outfp, pageptr1->begin, pageptr2->end);
  185.     }
  186.  
  187.     }
  188.  
  189.     /* Copy from the end of the last page to EOF into PAGEFILE. */
  190.  
  191.     pageptr1 = &docinfo->pages[num_pages - 1];
  192.     pscopy(infp, outfp, pageptr1->end + 1, docinfo->endtrailer);
  193.  
  194.     fclose(infp);
  195.     fclose(outfp);
  196. }
  197.  
  198.  
  199. /* Routine itoa(s, num) converts an integer, num, to an ascii string 
  200.  * representation of the integer, pointed to by s. The integer 100 is 
  201.  *  converted to the string "100", etc.
  202.  */
  203.  
  204. char *itoa(s, number)
  205. int number;
  206. char *s;
  207. {
  208.  
  209. /* Local variables:
  210.  *    i:        loop counter,
  211.  *    j:        number of loops needed to transpose the string in s,
  212.  *    body:        integer part of num/10,
  213.  *    remain:        remainder of num/10,
  214.  *    sign:        sign of the integer,
  215.  *    count:        number of times num is divisible by 10,
  216.  *    temp:        temporary character storage.
  217.  */
  218.  
  219.     int i, j, num, body, remain, sign, count;
  220.     char temp;
  221.  
  222.     count = 0;
  223.     num = number;
  224.     sign = 1;
  225.  
  226.     if (num == 0) {
  227.         s[count] = '0';
  228.         ++count;
  229.     } else {
  230.  
  231.         if (num < 0) {
  232.             sign = -1;
  233.             num = -num;
  234.         }
  235.  
  236.         while (num > 0) {        /* Divide by 10, convert */
  237.             body = (num/10);    /* remainder to ascii    */
  238.             remain = num - body*10;
  239.             s[count] = remain + '0';
  240.             num = body;
  241.             ++count;
  242.         }
  243.  
  244.         if (sign < 0) {
  245.             s[count] = '-';
  246.             ++count;
  247.         }
  248.  
  249.         /* Ascii representation is transposed in s, so put it in 
  250.          * the right order. */
  251.  
  252.         j = count/2;    
  253.  
  254.         for (i = 0; i < j; ++i) {
  255.             temp = s[i];
  256.             s[i] = s[count-(i+1)];
  257.             s[count-(i+1)] = temp;
  258.         }
  259.  
  260.     }
  261.  
  262.     s[count] = '\0';
  263.     return(s);
  264.  
  265. }
  266.